<?php


namespace app\workerman\controller;

use GatewayWorker\Lib\Gateway;
use app\server\UserServer;

use think\Env;


#use Redis;


class WorkerEvents
{
    /**
     * 当客户端连接时触发
     * 如果业务不需此回调可以删除onConnect
     *
     * @param int $client_id 连接id
     */
    public static function onConnect($client_id)
    {
        $data['type'] = 'get_client_id';
        $data['data'] = ['client_id' => $client_id];
    //     $data["user"] = UserServer::getUser();
    //     $register_address = Env::get('workerman.register_address', 'mysql');
    //     $data["register_address"] = $register_address;

    //     $config = config('redis');
    //     $redis = new Redis();
    //    # $redis->connect($config['host'], intval($config['port']), $config['timeout']);
    //     #pconnect($this->options['host'], intval($this->options['port']), $this->options['timeout'], 'persistent_id_' . $this->options['select']);
    //     if(!$config['persistent']){
    //         #密码怎么设置
    //         $redis->connect($config['host'], intval($config['port']), $config['timeout']);
    //     }else{
    //         $redis->pconnect($config['host'], intval($config['port']), $config['timeout'], 'persistent_id_' . $config['select']);
    //     }
       
    //     if ('' != $config['password']) {
    //         $redis->auth($config['password']);
    //     }

    //     if (0 != $config['select']) {
    //         $redis->select(intval($config['select']));
    //     }

    //     $redis->set('client_id',$client_id);
    //     $name = $redis->get('client_id');

    //     $data["name"] = $name;

        // 向当前client_id发送数据
        Gateway::sendToClient($client_id, json_encode($data, JSON_UNESCAPED_UNICODE));
    }

    /**
     * 当客户端发来消息时触发
     * @param int $client_id 连接id
     * @param mixed $message 具体消息
     */
    public static  function onMessage($client_id, $message)
    {
        dump("收到消息");
        dump($client_id);
        dump($message);

        //不作处理
    }

    /**
     * 当用户断开连接时触发
     * @param int $client_id 连接id
     */
    public static function onClose($client_id)
    {
        dump("断开连接".$client_id);
    }
}